<?php

/*
 * This class is used for as Model for Site table 
 */

class Model_Site extends Model_Base {

    protected $_name = 'tbl_site';
    protected $_primaryKey = 'site_id';

    
    
     //Get type data from tbl_list table

    public function gettypeArray($lang_id) {
        $ParentdropdownModel = new Model_Parentlist();
        $ParentdropdownArray = $ParentdropdownModel->fetchall('Type');
        $finalArray[''] = '-- Select Type --';
        $parent_id = $ParentdropdownArray[0]['parent_id'];
        $ChilddropdownModel = new Model_List();
        $where = 'child.lang_id = '.$lang_id.' and child.parent_id = '.$parent_id.' ';
        
        $ChilddropdownArray = $ChilddropdownModel->fetchType($where);

        foreach ($ChilddropdownArray as $com) {
            $finalArray[$com['ref_child_id']] = $com['child_name'];
        }
        if (!empty($finalArray)) {
            return $finalArray;
        } else {
            return array();
        }
    }

    
    
    
    //Company array
    public function getcompanyArray() {
        //company model
        $companyModel = new Model_Company();
       // $where = "com_type=1";
        $companyArray = $companyModel->fetchCompany();
        $finalArray[''] = '';
        foreach ($companyArray as $com) {
            $finalArray[$com['com_id']] = $com['com_name'];
        }
        return $finalArray;
    }

    //country Array
    public function getcountryArray() {
        //country model
        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchall();
        $finalArray[''] = '';
        foreach ($countryArray as $com) {
            $finalArray[$com['country_id']] = $com['country_name'];
        }
        return $finalArray;
    }

    /**
     * Function to return different status of the Site
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getStatusArray() {
        return array(
            #'' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    // static Guard Array
    public function getguardArray() {
        return array(
            '' => '-- Select Guard --',
            '0' => 'No',
            '1' => 'Yes'
        );
    }

    // static keysafe Array
    public function getkeysafeArray() {
        return array(
            '' => '-- Select Keysafe --',
            '0' => 'No',
            '1' => 'Yes'
        );
    }

    /**
     * Fetch an individual entry for view
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryByViewId($id,$lang_id) {
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('site' => 'tbl_site'))
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = site.updated_lgn_id")
                ->join(array('com' => 'tbl_company'), "com.com_id = site.com_id ",array('com_name'))
                ->joinLeft(array('type' => 'tbl_list'), "type.child_id = site.type_id and type.lang_id = ".$lang_id."")
                ->join(array('country' => 'tbl_country'), "country.country_id = site.site_country")
                ->where('site_id = ?', $id)
                ->where('site_status != 2');
        #echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     * Fetch an individual entry 
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('site' => 'tbl_site'))
                ->setIntegrityCheck(false)
                ->where('site_id = ?', $id)
                ->where('site_status != 2');

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get sites
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchSite($where = "", $limit = "", $offset = "0", $order = 'site_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('site' => 'tbl_site'))
                ->setIntegrityCheck(false)
                ->join(array('cmp' => 'tbl_company'), "cmp.com_id = site.com_id")
                ->where('site_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getSiteCount($where) {
        $select = $this->select()
                ->from(array('site' => 'tbl_site'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('cmp' => 'tbl_company'), "cmp.com_id = site.com_id")
                ->where('site_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete sites
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteSite($prdtArray) {

        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_site) {
                $siteData = Array();
                $siteData = $this->fetchEntryById($_site);
                if (count($siteData) > 0) {
                    $data = array('site_status' => '2');
                    $this->update($data, 'site_id =' . (int) $_site);
                }
            }
        } else {
            $site_id = $prdtArray;
            $siteData = $this->fetchEntryById($site_id);
            if (count($siteData) > 0) {
                $data = array('site_status' => '2');
                $this->update($data, 'site_id =' . $site_id);
            }
        }
    }
    
    
    //get week day name
    public function getweekArray() {
        return array(
            '1' => 'Monday',
            '2' => 'Tuesday',
            '3' => 'Wednesday',
            '4' => 'Thursday',
            '5' => 'Friday',
            '6' => 'Saturday',
            '7' => 'Sunday'
        );
    }
    
    
    //get 24 hours
    public function gethoursArray() {
        $hoursArray[''] = "Hours";
        foreach (range(1, 24) as $number) {
          $hoursArray[$number] = $number;
       }
           return $hoursArray;
    }
    
     //get minutes
    public function getminutesArray() {
       return array(
            '00' => '00',
            '15' => '15',
            '30' => '30',
            '45' => '45'
           
        );
    }
    
     public function fetchEntryByCompanyId($companyid) {
        $select = $this->select()
                ->from(array('site' => 'tbl_site'))
                ->setIntegrityCheck(false)
                ->where('com_id = ?', $companyid)
                ->where('site_status != 2');
        $data = parent::fetchAll($select);
        //echo $select;exit;
        $finalArray = array();
        foreach ($data as $key => $com) {
            $finalArray[$key] = $com['site_id'];
        }
            return $finalArray;
       
    }


}
